package com.xqledu.admin.entry.vo;

import com.xqledu.admin.entry.enumeration.UserEnum;
import com.xqledu.admin.entry.pojo.SysUser;
import lombok.AllArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 向其林
 * @version 1.0
 * @description: TODO
 * @date 2023/7/6 0006 21:51
 */
public class SysUserDetails implements UserDetails {

    private UserInfo user;

    private UserEnum userEnum;

    public SysUserDetails(UserInfo user, UserEnum userEnum) {
        this.user = user;
        this.userEnum = userEnum;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 获取用户的角色列表，并将其转换为 GrantedAuthority 对象
        List<GrantedAuthority> authorities = user.getRoles().stream()
                .map(role -> new SimpleGrantedAuthority(role.getUsername()))
                .collect(Collectors.toList());

        return authorities;
    }

    @Override
    public String getPassword() {
        return user.getUser().getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUser().getName();
    }

    @Override
    public boolean isAccountNonExpired() {
        // 根据你的业务需求，实现账号是否过期的判断逻辑
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        // 根据你的业务需求，实现账号是否被锁定的判断逻辑
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        // 根据你的业务需求，实现用户凭证是否过期的判断逻辑
        return true;
    }

    @Override
    public boolean isEnabled() {
        // 根据你的业务需求，实现用户是否可用的判断逻辑
        return user.getIsEnabled();
    }
}
